/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       campfire.h

	$Header: /game/campfire.h $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "campfire.h"

#include "adv_object_type.h"
#include "adventure_frame.h"
#include "adventure_sounds.h"
#include "army.h"
#include "basic_dialog.h"
#include "format_string.h"
#include "material_names.h"
#include "object_registration.h"
#include "music.h"
#include "player.h"
#include "random.h"
#include "replace_keywords.h"

static t_object_registration<t_campfire> k_registration( k_adv_object_campfire );

// -------------------------------------------------------
// campfire.  Gives gold and materials.
// -------------------------------------------------------
t_campfire::t_campfire( t_stationary_adventure_object const& source )
          : t_stationary_adventure_object( source )
{
	m_gold = random( 3, 5 ) * 100;
	m_material.material = random( k_wood, k_gems );
	m_material.amount = m_gold / 100;
	if (m_material.material <= k_ore)
		m_material.amount *= 2;
}


// -------------------------------------------------------
// campfire.  Gives gold and materials.
// -------------------------------------------------------
void t_campfire::activate_trigger( t_army* army, t_adv_map_point const& point, 
		                           t_direction direction, t_adventure_frame* frame )
{
	t_player* owner = army->get_owner();

	owner->gain( k_gold, m_gold );
	owner->gain( m_material.material, m_material.amount );
	if (!army->get_owner()->is_computer())
	{
		t_screen_point      screen_point;
		t_basic_dialog*     dialog;
		std::string         text;
		t_material_array	rewards;

		// initialize dialog
		dialog = new t_basic_dialog;
		text = get_text( "initial" );
		rewards[k_gold] = m_gold;
		rewards[m_material.material] = m_material.amount;
		text = replace_keywords( text, rewards );
		dialog->set_text( text );

		dialog->add_materials( rewards );
		dialog->add_ok_button();

		frame->update_funds();
		dialog->set_title( get_name() );
		dialog->open( get_dialog_bottom( point ), true, k_align_bottom );
		t_sound_cache music = get_music_playing();
		stop_music();
		get_dialog_sound( k_dialog_sound_treasure )->play( get_sound_volume() );
		dialog->run_modal();
		play_music( music );
	}
	destroy();
}

// -------------------------------------------------------
// campfire.  Gives gold and materials.
// -------------------------------------------------------
bool t_campfire::write( std::streambuf& stream ) const
{
	put<int>( stream, m_gold );
	::write( stream, &m_material, sizeof(m_material));
	return true;
}

// -------------------------------------------------------
// campfire.  Gives gold and materials.
// -------------------------------------------------------
bool t_campfire::read( std::streambuf&						stream, 
					   t_qualified_adv_object_type const&	type,
					   int									version ) 
{
	m_gold = get<int>( stream );
	::read( stream, &m_material, sizeof(m_material));
	return true;
}

// -------------------------------------------------------
// campfire.  Gives gold and materials.
// -------------------------------------------------------
float t_campfire::ai_value( t_adventure_ai const& ai, t_creature_array const& army, int move_cost ) const
{
	t_player* player = army.get_owner();
	t_material_values const& values = player->get_material_values();
	
	return values[k_gold] * m_gold + values[m_material.material] * m_material.amount;
}
